package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.DateDto;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.ArrayList;

@Service(interfaceClass =OrderSettingService.class )
//@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
     private OrderSettingDao orderSettingDao;
    //定义一下插入的数组(集合对象)

    @Override
    @Transactional
    public Result batchAdd(List<String[]> list) {
        //
        ArrayList<OrderSetting> addList = new ArrayList<>();
        ArrayList<OrderSetting> addList2 = new ArrayList<>();
        ArrayList<OrderSetting> updateList = new ArrayList<>();
        list.stream().forEach(strings -> {
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setNumber(0);
            for (int i = 0; i < strings.length; i++) {
                if (i==0){
                    try {
                        orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0]));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    try {
                        orderSetting.setReservations(Integer.parseInt(strings[1]));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            addList.add(orderSetting);
        });
        for (OrderSetting setting : addList) {
            Date date1 = setting.getOrderDate();
            java.sql.Date sqlDate = new java.sql.Date(date1.getTime());
            Integer id = orderSettingDao.findByDate(sqlDate);
            if (id!=null && id>0){
                //里面这一条里面有了 应该进行更新操作 不应该进行删除
                updateList.add(setting);
            }else {
                addList2.add(setting);
            }
        }
        //走到这一步addList 都是需要插入的  updateList 是需要更新的

        //接下来把addList 插入数据库、//返回结果是影响的行数  由事务进行判断 是否成功
        try {
            updateList.forEach(orderSetting -> {
                orderSettingDao.update(orderSetting.getReservations(),new java.sql.Date(orderSetting.getOrderDate().getTime()));
            });
            Boolean aBoolean = orderSettingDao.betchAdd(addList2);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @Override
    public Result getOrderSettingByMonth(String month) {
        String s1 =month+"-01";
        String s2 =month+"-31";
        Date dates1 = null;
        Date dates2 = null;
        try {
            //这个地方 坑死了   想摔键盘
            dates1 = DateUtils.parseString2Date(s1,"yyyy-M-dd");
            dates2 = DateUtils.parseString2Date(s2,"yyyy-M-dd");
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.sql.Date date1 = new java.sql.Date(dates1.getTime());
        java.sql.Date date2 = new java.sql.Date(dates2.getTime());
        ArrayList<OrderSetting> orderSettingList = orderSettingDao.findBymonth(date1, date2);
        ArrayList<DateDto> reslist = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            DateDto dateDto = new DateDto();
            //方法过时了 暂时没有想到方法  不能用 是错误的方法
            int day = orderSetting.getOrderDate().toInstant().atZone(ZoneId.systemDefault()).getDayOfMonth();
            dateDto.setDate(day);
            dateDto.setNumber(orderSetting.getNumber());
            dateDto.setReservations(orderSetting.getReservations());
            reslist.add(dateDto);
        }
        Result result = new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS);
        result.setData(reslist);
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
            Date orderDate = orderSetting.getOrderDate();
        Integer count = orderSettingDao.findByDate((java.sql.Date) orderDate);
        if(count > 0){
                orderSettingDao.editNumberByOrderDate(orderSetting);
            }else{

                ArrayList<OrderSetting>list=new ArrayList<>();
                orderSettingDao.betchAdd(list);
            }
        }
    }


